var DOMAIN = 'http://localhost:8848';

var finalFileList = []

var login = function () {
    let ajax = new XMLHttpRequest();
    ajax.open('POST',DOMAIN+'/login',true);
    ajax.setRequestHeader('contentType','application/json');
    let userInfo = {
        userName:'user',
        userPaswod:'paswod',
    };
    ajax.send(userInfo);
};

var getImg = function () {

};

var postImg = function () {
    let imgs = document.querySelectorAll(".obj");
    for (let i = 0; i < imgs.length; i++) {
        setTimeout(new FileUpload(imgs[i].file),500);
    }
};

var preview = function (fileList) {
    for (let i = 0; i < fileList.length; i++){
        let file = fileList[i];
        let imageType = /^image\//;
        if (!imageType.test(file.type)){
            continue;
        }
        let imgInfo = document.createElement('div');
        let img = document.createElement('img');
        let txt = document.createElement('span');
        txt.innerText = file.name;
        img.classList.add('obj');
        img.file = file;
        let imgContainer = document.getElementById('imgContainer');
        imgInfo.appendChild(img);
        imgInfo.appendChild(txt);
        imgInfo.id = 'imgInfo';
        imgContainer.appendChild(imgInfo);

        let reader = new FileReader();
        reader.onload = (function (img) {
            return function (e) {
                    img.src = e.target.result;
            }
        })(img);
        reader.readAsDataURL(file);
        finalFileList = fileList;
    }
};

var selectFile = function () {
  let fileInput = document.getElementById('fileInput');
  fileInput.click();
};

function FileUpload(file) {
    console.log(file.name);
    let formData = new FormData();
    formData.append('file',file);
    let xhr = new XMLHttpRequest();
    xhr.open("POST", DOMAIN+'/post/img');
    //xhr.overrideMimeType('text/plain; image/jpeg');
    //xhr.setRequestHeader('Content-Type','image/jpeg');
    // xhr.setRequestHeader('Access-Control-Allow-Origin','*');
    // xhr.withCredentials=true;
    //xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xhr.send(formData);
}
